var LOGIN, REGISTER, FORGOT;
function checkmail(email){
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
	var returnval=emailfilter.test(email)
	return returnval;
}

$(document).ready(function(){
LOGIN={
	kiemtranhap:function(ops){
		var email = ops.email;
		var password = ops.password;
		//alert(password);return false;
		if(email && password!='da39a3ee5e6b4b0d3255bfef95601890afd80709'){
			if(!checkmail(email)){
				$('p.wanning').html('* Email không hợp lệ');
				$('p.wanning').fadeOut();
				$('p.wanning').fadeIn();
				document.getElementById('email').focus();
				return false;
			}else{
				$.ajax({
					url:ROOT+'user/login/loginxuly',
					type:'post',
					data:{email:email, password:password},
					success:function(data){
						var obj = jQuery.parseJSON(data);
						var kq = obj.ketqua;
						if(kq==0){
							$('p.wanning').html('* Email hoặc mật khẩu không đúng');
							$('p.wanning').fadeOut();$('p.wanning').fadeIn();$('p.wanning').fadeOut();$('p.wanning').fadeIn();
							document.getElementById('email').focus();
						}
						if(kq==1){
							$('p.wanning').html('');
							alert('Đăng nhập thành công');
							window.location = ROOT+'user';
						}
					}
				});
			}
		}
		else{
			$('p.wanning').html('* Chưa nhập đủ thông tin');
			$('p.wanning').fadeOut();$('p.wanning').fadeIn();$('p.wanning').fadeOut();$('p.wanning').fadeIn();
			document.getElementById('email').focus();
			return false;
		}
	},
	test:function(){
		alert('test');
		return false;
	}
}

REGISTER={
	kiemtradangky:function(ops){
		var firstname = ops.firstname;
		var lastname = ops.lastname;
		var email = ops.email;
		var password = ops.password;
		var confpassword = ops.confpassword;
		var sex = ops.sex;
		var lengthpass = ops.lengthpass;
		var captcha = ops.captcha;
		//var captcha_session = ops.captcha_session;
		//alert(captcha);
		//alert(captcha_session);
		//return false;

		//alert(firstname+'   '+lastname+'   '+email+'   '+password+'   '+confpassword+'   '+sex);
		//return false;
		if(firstname && lastname && email && password != 'da39a3ee5e6b4b0d3255bfef95601890afd80709' && confpassword != 'da39a3ee5e6b4b0d3255bfef95601890afd80709' && sex!=-1 && captcha){
			if(!checkmail(email)){
				$('p.wanning').html('* Email không hợp lệ');
				$('p.wanning').fadeOut();$('p.wanning').fadeIn();$('p.wanning').fadeOut();$('p.wanning').fadeIn();
				document.getElementById('email').focus();
			}else{
				if(lengthpass < 6){
					$('p.wanning').html('* Mật khẩu phải có tối thiểu 6 ký tự');
					$('p.wanning').fadeOut();$('p.wanning').fadeIn();$('p.wanning').fadeOut();$('p.wanning').fadeIn();
					document.getElementById('password').focus();
				}else{
					if(password != confpassword){
						$('p.wanning').html('* 2 mật khẩu không giống nhau');
						$('p.wanning').fadeOut();$('p.wanning').fadeIn();$('p.wanning').fadeOut();$('p.wanning').fadeIn();
						document.getElementById('confpassword').focus();
					}else{
						$.ajax({
							url:ROOT+'user/register/registerxuly',
							type:'post',
							data:{captcha:captcha, firstname:firstname, lastname:lastname, email:email, password:password, sex:sex},
							success:function(data){
								var obj = jQuery.parseJSON(data);
								var kq = obj.ketqua;
								if(kq == -1){
									$('p.wanning').html('* Mã Captcha chưa đúng');
									$('p.wanning').fadeOut();$('p.wanning').fadeIn();$('p.wanning').fadeOut();$('p.wanning').fadeIn();
									document.getElementById('captcha').focus();
								}else if(kq == 0){
									$('p.wanning').html('* Email đã có người đăng ký');
									$('p.wanning').fadeOut();$('p.wanning').fadeIn();$('p.wanning').fadeOut();$('p.wanning').fadeIn();
									document.getElementById('email').focus();
								}else{
									$('p.wanning').html('');
									alert('Đăng ký thành công');
									window.location = ROOT+'user';
								}
							}
						});
					}
					
				}
			}
		}else{
			$('p.wanning').html('* Chưa nhập đủ thông tin');
			$('p.wanning').fadeOut();$('p.wanning').fadeIn();$('p.wanning').fadeOut();$('p.wanning').fadeIn();
			document.getElementById('firstname').focus();
		}
	}
}

FORGOT={
	laylaimatkhau:function(ops){
		//alert(ops.captcha);
		//return false;
	if(ops.email==""){
		alert('Email không được để trống');
		return false;
	}
	else if(!checkmail(ops.email)){
		alert('Email không đúng định dạng');
		return false;
	}else{
			$.ajax({
				url:ROOT+'user/forgot/forgotxuly',
				type:'post',
				data:{user_email:ops.email, captcha:ops.captcha},
				success:function(data){
					alert(data);
				}
			});
		}
	}
}

$('.btloadcaptcha').click(function(){
	$('.imgcaptcha').attr("src","http://localhost/hoidap/Source/application/modules/captcha/captcha.php");
	alert('aa');
});

$('#formlogin').submit(function(){
	return false;
});
$('#formregister').submit(function(){
	return false;
});
$('#formforgotpass').submit(function(){
	return false;
});

});